package sdl_impl

import "github.com/banthar/Go-SDL/sdl"

type Keyboard struct {
	keyMap       map[sdl.Keysym]uint8
	down         [16]bool
	aKeyIsQueued bool
	queue        uint8
}

func NewKeyboard() *Keyboard {
	return &Keyboard{}
}

func (k *Keyboard) SetKeyMap(keys map[sdl.Keysym]uint8) {
	k.keyMap = keys
}

func (k *Keyboard) KeyDown(which uint8) bool {
	return k.down[int(which)]
}

func (k *Keyboard) LastKey() (bool, uint8) {
	return k.aKeyIsQueued, k.queue
}

func (k *Keyboard) ClearLastKey() { k.aKeyIsQueued = false }

func (k *Keyboard) HandleKeyDown(key sdl.Keysym) {
	if key, hasIt := k.keyMap[key]; hasIt {
		k.down[key] = true
		if !k.aKeyIsQueued {
			k.queue = key
			k.aKeyIsQueued = true
		}
	}
}

func (k *Keyboard) HandleKeyUp(key sdl.Keysym) {
	if key, hasIt := k.keyMap[key]; hasIt {
		k.down[key] = false
	}
}
